package cn.edu.zafu.bigdata.liuwenyi;

import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame {
    // 设置游戏的最大猜测次数
    private static final int MAX_ATTEMPTS = 5;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        boolean playAgain = true;

        System.out.println("欢迎来到猜数字游戏！");

        while (playAgain) {
            int numberToGuess = random.nextInt(100) + 1;  // 生成1到100的随机数
            int attempts = 0;
            boolean guessedCorrectly = false;

            System.out.println("我已经想好了一个1到100之间的数字。你有 " + MAX_ATTEMPTS + " 次机会来猜它。");

            while (attempts < MAX_ATTEMPTS && !guessedCorrectly) {
                System.out.print("请输入你的猜测：");
                int userGuess = scanner.nextInt();
                attempts++;

                if (userGuess < numberToGuess) {
                    System.out.println("太小了！");
                } else if (userGuess > numberToGuess) {
                    System.out.println("太大了！");
                } else {
                    guessedCorrectly = true;
                    System.out.println("恭喜你！你猜对了！");
                }

                System.out.println("你还有 " + (MAX_ATTEMPTS - attempts) + " 次机会。");
            }

            if (!guessedCorrectly) {
                System.out.println("很遗憾，你没有猜对。正确的数字是：" + numberToGuess);
            }

            // 提问是否要再玩一轮
            System.out.print("你想再玩一轮吗？(y/n): ");
            char response = scanner.next().toLowerCase().charAt(0);

            if (response != 'y') {
                playAgain = false;
            }
        }

        System.out.println("感谢你的游戏，再见！");
        scanner.close();
    }
}
